package com.itheima.Task;

import com.itheima.common.constant.RedisConstant;
import com.itheima.common.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@Slf4j
public class ALiOssImage {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Scheduled(cron = "0 0 1 * * ? ")
    public void deleteImage(){
        log.info("定时任务开始执行...");
        //计算redis中两个set集合的差值
        Set<String> diff = redisTemplate.opsForSet().difference(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if(diff != null && diff.size() > 0){
            for (String pic : diff) {
                //删除图片
                aliOssUtil.delete(pic);
                //删除redis中多余的图片名称
                redisTemplate.opsForSet().remove(RedisConstant.SETMEAL_PIC_RESOURCES,pic);
            }


        }
    }
}
